package com.atguigu.aliyunvod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.aliyunvod.service.VodService;
import com.atguigu.aliyunvod.util.AliyunVodSDKUtils;
import com.atguigu.aliyunvod.util.ConstantVodUtils;
import com.atguigu.common.util.R;
import com.atguigu.service.base.exception.GuliException;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.ApiParam;
import org.bouncycastle.jcajce.provider.symmetric.RC2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/vod")
@CrossOrigin
public class VodController {

    private static final String KEY_ID = "LTAI5tPjGEiTMfPpN19KHGhB";
    private static final String KEY_SECRET = "uBm221yuzyOpfc4RrsKCUXtSbgFiEa";

    @Autowired
    private VodService vodService;

    @PostMapping("/upload")
    public R upload(MultipartFile file) {
        String videoSourceId = vodService.uploadVideo(file);
        System.out.println("上传视频返回值：" + videoSourceId);
        return R.ok().data("videoSourceId", videoSourceId).data("videoId", videoSourceId);
    }

    @PostMapping("/delete/{videoSourceId}")
    public R removeVideo(@ApiParam(name = "videoSourceId", value = "云端视频id", required = true)
                         @PathVariable String videoSourceId) {
        System.out.println("删除视频：" + videoSourceId);
        vodService.removeVideo(videoSourceId);
        return R.ok().message("视频删除成功");
    }

    //删除多个阿里云视频
    @DeleteMapping("/deleteBatch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        vodService.removeAllVideo(videoIdList);
        return R.ok();
    }

    @GetMapping("/getPlayInfo/{videoSourceId}")
    public R getPlayInfo(@PathVariable String videoSourceId) {
        System.out.println("获取视频信息：" + videoSourceId);
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(KEY_ID, KEY_SECRET);
            GetPlayInfoRequest req = new GetPlayInfoRequest();
            req.setVideoId(videoSourceId);
            GetPlayInfoResponse res = client.getAcsResponse(req);
            System.out.println(res.getVideoBase().getTitle());
            List<GetPlayInfoResponse.PlayInfo> playInfoList = res.getPlayInfoList();
            GetPlayInfoResponse.PlayInfo playInfo = playInfoList.get(0);
            System.out.println(playInfo.getPlayURL());
            return R.ok().data("playInfo", playInfo).data("playURL", playInfo.getPlayURL());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"视频playInfo获取失败");
        }
    }

    //根据视频id获得视频凭证
    @GetMapping("/getPlayAuth/{videoSourceId}")
    public R getPlayAuth(@PathVariable String videoSourceId) {
        System.out.println("获取视频凭证：" + videoSourceId);
        try {
            //创建初始化对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ConstantVodUtils.ACCESS_KEY_ID,
                    ConstantVodUtils.ACCESS_KEY_SECRET
            );
            //创建获取凭证的request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request对象中设置视频id
            request.setVideoId(videoSourceId);

            //调用方法获得凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth", playAuth);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"视频playAuth获取失败");
        }
    }

    @GetMapping("/test/error")
    @HystrixCommand(groupKey = "test", fallbackMethod = "fallback01")
    public R testHystrix(String param) {
        int a = 10 / 0 ;
        return R.ok();
    }

    private R fallback01(){
        return R.error().message("test/error");
    }
}
